//
//  WPDiagnosisPictureCell.m
//  wobingwoyi
//
//  Created by WGP on 2016/11/30.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPDiagnosisPictureCell.h"
#import "WPContentModel.h"

@interface WPDiagnosisPictureCell ()

@end
@implementation WPDiagnosisPictureCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self showBigImageBtn];
        [self iconView];
        
//        self.contentView.backgroundColor = [UIColor yellowColor];
    }
    return self;
}

-(UIButton *)showBigImageBtn
{
    if (_showBigImageBtn == nil)
    {
        _showBigImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_showBigImageBtn setBackgroundImage:[UIImage imageNamed:@"showBigImage_bg"] forState:UIControlStateNormal];
        [_showBigImageBtn addTarget:self action:@selector(showBigImageBtnDidClick) forControlEvents:UIControlEventTouchDown];
        [self.contentView addSubview:_showBigImageBtn];
        
        [_showBigImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.contentView).offset(10);
            make.top.equalTo(self.contentView).offset(5);
        }];
    }
    return _showBigImageBtn;
}

-(UIImageView *)iconView
{
    if (_iconView == nil)
    {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder_Image"]];
        _iconView.contentMode = UIViewContentModeScaleAspectFill;
        _iconView.clipsToBounds = YES;
        _iconView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pictureDidTap:)];
        [_iconView addGestureRecognizer:tgr];
        
        [self.contentView addSubview:_iconView];
        
        CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
        CGFloat itemHeight = itemWidth*90/112.5;
        
        UIImage *image = [UIImage imageNamed:@"showBigImage_bg"];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(10);
            make.top.equalTo(self.contentView).offset(image.size.height+10);
            make.width.mas_equalTo(itemWidth);
            make.height.mas_equalTo(itemHeight);
           
        }];
    }
    return _iconView;
}

-(void)setUrl:(NSString *)url
{
    _url = url;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder_Image"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        self.model.scale = image.size.width/image.size.height;
    }];
    
    if (self.isShowBigImage)
    {
        UIImage *image = [UIImage imageNamed:@"showBigImage_bg"];
        
        [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.model.width);
            make.height.mas_equalTo(self.model.height-image.size.height-10);
        }];
    }
    else
    {
        [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.model.width);
            make.height.mas_equalTo(self.model.height);
        }];
    }
}

-(void)setIsShowBigImage:(BOOL)isShowBigImage
{
    _isShowBigImage = isShowBigImage;
    
    UIImage *image = [UIImage imageNamed:@"showBigImage_bg"];
    
    
    if (isShowBigImage)
    {
        
        CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
        
        if (self.model.width == itemWidth)
        {
            CGFloat itemHeight = itemWidth*90/112.5;
            self.model.height = itemHeight + image.size.height+15;
            
            [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).offset(image.size.height+10);
                make.height.mas_equalTo(itemHeight);
            }];
        }
        else
        {
            itemWidth = SCREEN_WIDTH-2*10;
            CGFloat itemHeight = itemWidth/self.model.scale;
            self.model.height = itemHeight + image.size.height+15;
            
            [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).offset(image.size.height+10);
                make.height.mas_equalTo(itemHeight);
            }];
        }
    }
    else
    {
        CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
        
        if (self.model.width == itemWidth)
        {
            CGFloat itemHeight = itemWidth*90/112.5;
            self.model.height = itemHeight + 10;
            
            [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).offset(5);
                make.height.mas_equalTo(itemHeight);
            }];
        }
        else
        {
            itemWidth = SCREEN_WIDTH-2*10;
            CGFloat itemHeight = itemWidth/self.model.scale;
            self.model.height = itemHeight + 10;
            
            [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).offset(5);
                make.height.mas_equalTo(itemHeight);
            }];
        }
    }
}

-(void)showBigImageBtnDidClick
{
    //展示大图
//    if (self.showAllBigPicture)
//    {
//        self.showAllBigPicture();
//    }
    
    [self pictureDidTap:nil];
}

-(void)pictureDidTap:(UITapGestureRecognizer *)tgr
{
    //点击图片
    UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:self.url];
    
    if (!image)
    {
        self.iconView.image = [UIImage imageNamed:@"image_failed"];
        return ;
    }
    
    CGFloat scale = image.size.width/image.size.height;
    
    CGFloat itemWidth = SCREEN_WIDTH-2*10;
    CGFloat itemHeight = itemWidth/scale;
    
    if (self.model.width == itemWidth)
    {
        if (self.showAllPicture)
        {
            self.showAllPicture(self.url);
        }
    }
    else
    {
        self.model.width = itemWidth;
        self.model.height = itemHeight;
        
        if (self.refreshCell)
        {
            self.refreshCell(self.indexPath);
        }
    }
    
}

@end
